﻿using System.Collections.Generic;

namespace AddressManager.Common.Extension
{
    public static class ListExtension
    {
        /// <summary>
        /// <para>Replaces one item of a collection with a new item</para>
        /// <para>Checks if parameters not null because collection items should not be null</para>
        /// <para>Example: EditionList.Replace(editionToReplace, editionToInsert)</para>
        /// </summary>
        /// <typeparam name="T">Type of the calling collection</typeparam>
        /// <param name="list">Calling collection</param>
        /// <param name="itemToReplace">Item to replace</param>
        /// <param name="itemToInsert">Item to insert</param>
        public static void Replace<T>(this IList<T> list, T itemToReplace, T itemToInsert) where T : class
        {
            if (itemToReplace != null && itemToInsert != null)
            {
                var pos = list.IndexOf(itemToReplace);
                list.Remove(itemToReplace);
                list.Insert(pos, itemToInsert);
            }
        }

        public static List<T> AddAll<T>(this List<T> source, IEnumerable<T> entriesToAdd)
        {
            entriesToAdd.ForEach(source.Add);
            return source;
        }
    }
}
